#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAXNUM 512 //地铁最大站数
#define MAXNAMELEN 16 //地铁站名字符串最大长度
#define INFINITY 32767
#define ERROR -1
typedef int Vertex;
typedef struct SNode Station;
struct SNode
{
    char sname[MAXNAMELEN]; //站名
    int ischange; //是否为换乘站，0为不是换乘站，1为是换乘站
};
typedef struct WNode Weight;
struct WNode
{
    int wei; //两个站之间的权重，即相差的站数
    int ino; //两个顶点所在的线号
};

Station BGvertex[MAXNUM]; //地铁网的顶点
Weight BGweights[MAXNUM][MAXNUM]; //无向图邻接矩阵
int Vnum = 0; //实际地铁总站数

int AddVertex(Station p)
{
    if (!p.ischange) //不是换乘站
    {
        BGvertex[Vnum++] = p;
        return Vnum - 1;
    }
    else
    {
        int i;
        for (i = 0; i < Vnum; i++) //查找换乘站
            if (!strcmp(p.sname, BGvertex[i].sname))
                return i;
        BGvertex[Vnum++] = p;
        return Vnum - 1;
    }
}

void Init_Map()
{
    FILE *IN;
    int snum, Ino, Inum, v1, v2;
    Station st;
    if((IN = fopen("bgstations.txt", "r")) == NULL)
        printf("Cannot open this file!\n");
    fscanf(IN, "%d", &snum);
    for(int i=0;i<snum;i++)
    {
        fscanf(IN, "%d %d", &Ino, &Inum);
        v1 = v2 = -1;
        for(int j=0;j<Inum;j++)
        {
            fscanf(IN, "%s %d", st.sname, &st.ischange);
            v2 = AddVertex(st); //将st加入到顶点数组内并返回数组下标，其中我们用BGvertex数组的下标来表示顶点的编号
            if (v1 != -1)
            {
                BGweights[v1][v2].wei = BGweights[v2][v1].wei = 1; //相邻顶点权重为1
                BGweights[v1][v2].ino = BGweights[v2][v1].ino = Ino; //同一线路，线路号相同
            } //无向图，v1->v2和v2->v1都要存在邻接矩阵里
            v1 = v2;
        }
    }
    for(int i=0;i<Vnum;i++)
        for(int j=0;j<Vnum;j++)
        {
            if(!BGweights[i][j].wei)
                BGweights[i][j].wei = INFINITY;
            if(!BGweights[i][j].ino)
                BGweights[i][j].ino = -1;
        }
    fclose(IN);
}

void PrintPath(int v0, int v1, int spath[])
{
    char path[80] = {0}, buf[80];
    int board[80], bcount = 0, line = -1, sc = 0;
    int i;
    do{
        board[bcount++] = v1;
    }while((v1 = spath[v1]) != v0);
    board[bcount++] = v0;
    line = BGweights[board[bcount - 1]][board[bcount - 2]].ino;
    sprintf(buf, "%s-%d(", BGvertex[board[bcount - 1]].sname, line);
    strcpy(path, buf);
    sc = 1;
    for(i=bcount-2;i>0;i--,sc++)
        if(BGweights[board[i]][board[i - 1]].ino != line) //换乘，新的线路号
        {
            line = BGweights[board[i]][board[i - 1]].ino;
            sprintf(buf, "%d)-%s-%d(", sc, BGvertex[board[i]].sname, line);
            strcat(path, buf);
            sc = 0;
        }
    sprintf(buf, "%d)-%s\n", sc, BGvertex[board[i]].sname);
    strcat(path, buf);
    puts(path);
}

Vertex FindMinDist(int dist[], int collected[])
{
    Vertex MinV, V;
    int MinDist = INFINITY;
    for(V=0;V<Vnum;V++)
    {
        if(collected[V] == 0 && dist[V] < MinDist)
        {
            MinDist = dist[V];
            MinV = V;
        }
    }
    if(MinDist < INFINITY)
        return MinV;
    else
        return ERROR;
}

void Dijkstra(Vertex S, Vertex S1, int dist[], int spath[])
{
    int collected[MAXNUM];
    Vertex V, W;
    for(V=0;V<Vnum;V++)
    {
        dist[V] = BGweights[S][V].wei;
        if (dist[V] < INFINITY)
            spath[V] = S;
        else
            spath[V] = -1;
        collected[V] = 0;
    }
    /* 先将起点收入集合 */
    dist[S] = 0;
    collected[S] =1;
    while(1)
    {
        if(collected[S1] == 1) break; //收录目的地后就跳出
        V = FindMinDist(dist, collected);
        if (V == ERROR)
            break;
        collected[V] = 1; 
        for(W=0; W<Vnum;W++)
            if(collected[W] == 0 && BGweights[V][W].wei < INFINITY)
                if(dist[V] + BGweights[V][W].wei < dist[W])
                {
                    dist[W] = dist[V] + BGweights[V][W].wei;
                    spath[W] = V;
                }
    }
}

int main()
{
    char ap1[MAXNAMELEN], ap2[MAXNAMELEN];
    scanf("%s %s", ap1, ap2);
    int v0 = -1, v1 = -1, i;
    Init_Map();
    for (i=0;i<Vnum;i++)
    {
        if(!strcmp(ap1, BGvertex[i].sname))
            v0 = i;
        if(!strcmp(ap2, BGvertex[i].sname))
            v1 = i;
    }
    int spath[MAXNUM] = {0};
    int dist[MAXNUM] = {0};
    Dijkstra(v0, v1, dist, spath);
    PrintPath(v0, v1, spath);
    return 0;
}